/*
 * ModofierGroupEditor.java
 *
 * Created on August 4, 2006, 12:28 AM
 */
package com.floreantpos.ui.model;

import org.hibernate.Hibernate;
import org.hibernate.Session;

import com.floreantpos.model.MenuModifierGroup;
import com.floreantpos.model.dao.ModifierDAO;
import com.floreantpos.model.dao.ModifierGroupDAO;
import com.floreantpos.swing.FixedLengthDocument;
import com.floreantpos.swing.MessageDialog;
import com.floreantpos.ui.BeanEditor;
import com.floreantpos.util.POSUtil;

/**
 *
 * @author MShahriar
 */
public class MenuModifierGroupForm extends BeanEditor {

  /**
   * Creates new form ModofierGroupEditor
   */
  public MenuModifierGroupForm() throws Exception {
    this(new MenuModifierGroup());
  }

  public MenuModifierGroupForm(MenuModifierGroup group) throws Exception {
    initComponents();

    tfName.setDocument(new FixedLengthDocument(60));

    setBean(group);
  }

  private void initComponents() {
    jLabel1 = new javax.swing.JLabel();
    tfName = new com.floreantpos.swing.FixedLengthTextField();

    jLabel1.setText(com.floreantpos.POSConstants.NAME + ":");

    tfName.setText("fixedLengthTextField1");

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jLabel1)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(tfName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
                    .addContainerGap())
    );
    layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel1)
                            .add(tfName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
  }

  private javax.swing.JLabel jLabel1;
  private com.floreantpos.swing.FixedLengthTextField tfName;

  @Override
  public boolean save() {
    try {
      if (!updateModel()) {
        return false;
      }

      MenuModifierGroup group = (MenuModifierGroup) getBean();

      ModifierGroupDAO dao = new ModifierGroupDAO();
      dao.saveOrUpdate(group);
    } catch (Exception e) {
      MessageDialog.showError(e);
      return false;
    }
    return true;
  }

  @Override
  public void dispose() {
  }

  @Override
  protected void updateView() {
    MenuModifierGroup group = (MenuModifierGroup) getBean();

    if (group.getId() != null && !Hibernate.isInitialized(group.getModifiers())) {
      ModifierDAO dao = new ModifierDAO();
      Session session = dao.getSession();
      group = (MenuModifierGroup) session.merge(group);
      Hibernate.initialize(group.getModifiers());
      session.close();
    }

    tfName.setText(group.getName());
  }

  @Override
  protected boolean updateModel() {
    MenuModifierGroup group = (MenuModifierGroup) getBean();

    String name = tfName.getText();
    if (POSUtil.isBlankOrNull(name)) {
      MessageDialog.showError(com.floreantpos.POSConstants.NAME_REQUIRED);
      return false;
    }

    group.setName(name);

    return true;
  }

  public String getDisplayText() {
    MenuModifierGroup modifierGroup = (MenuModifierGroup) getBean();
    if (modifierGroup.getId() == null) {
      return com.floreantpos.POSConstants.NEW_MODIFIER_GROUP;
    }
    return com.floreantpos.POSConstants.EDIT_MODIFIER_GROUP;
  }
}
